T = 3;

market_code = 'VNINDEX';
number_of_stocks = 20;
stock_codes = []; 
porfolio_weights = [];

temp = zeros(T, number_of_stocks);

% ME
olsme = temp;
bayesme = temp;
medme = temp;
trimme = temp;
weightme = temp;
shirnkageme = temp;
smoothingtme = temp;
kalmanme = temp;
tobitme = temp;
trunme = temp;
gmmme = temp;

% MSE
olsmse = temp;
bayesmse = temp;
medmse = temp;
trimmse = temp;
weightmse = temp;
shirnkagemse = temp;
smoothingtmse = temp;
kalmanmse = temp;
tobitmse = temp;
trunmse = temp;
gmmmse = temp;

% MAD
olsmad = temp;
bayesmad = temp;
medmad = temp;
trimmad = temp;
weightmad = temp;
shirnkagemad = temp;
smoothingtmad = temp;
kalmanmad = temp;
tobitmad = temp;
trunmad = temp;
gmmmad = temp;

% R^2
olsrsquare = temp;
bayesrsquare = temp;
medrsquare = temp;
trimrsquare = temp;
weightrsquare = temp;
shirnkagersquare = temp;
smoothingtrsquare = temp;
kalmanrsquare = temp;
tobitrsquare = temp;
trunrsquare = temp;
gmmrsquare = temp;
tic;
for j = 1:T
    t = j
%     [portfolio_codes porfolio_weights portfolio_date portfolio_close_price portfolio_volume] ...
%         = getPortfolio(market_code, number_of_stocks, stock_codes, porfolio_weights);
    [portfolio_codes porfolio_weights portfolio_date portfolio_close_price portfolio_volume] = loadPortfolio(market_code, number_of_stocks, j);
    
    pStock = portfolio_close_price(:, 2:end);
    pIndex = portfolio_close_price(:, 1);
    
    %get return of Stocks and Index
    rStock = getReturn(pStock);
    rIndex = getReturn(pIndex);
    
    for i=1:number_of_stocks
        olsbeta(i) = BetaOLS(rStock(:,i),rIndex);

        bayesbeta(i) = BetaBayes(rStock(:,i),rIndex, 'HOSE','VNINDEX');
        %bayesbeta = BetaBayes(rStock,rIndex, 'HNX','HNXINDEX')

        medrobustbeta(i) = BetaRobustMed(rStock(:,i),rIndex);

        trimrobustbeta(i) = BetaRobustTrim(rStock(:,i),rIndex);

        weightrobustbeta(i) = BetaRobustWeight(rStock(:,i),rIndex,'andrews');

        shrinkagebeta(i) = BetaShrinkage(rStock(:,i),rIndex);

        smoothingbeta = BetaSmoothing(rStock(:,i), rIndex, 0.97);

        kalmanfilterbeta = BetaKalman(rStock(:,i), rIndex, 1);

        %kalmanfilterbeta = BetaKalman(rStock(:,i), rIndex, 2);

        tobitmodelbeta(i) = BetaTobit(rStock(:,i), rIndex, 0.05);

        truncatedbeta(i) = BetaTruncated(rStock(:,i), rIndex, 0.05);

        gmmbeta(i) = BetaGMM(pStock(:,i),pIndex,0.05,-0.05);

        %ME
        olsme(j, i) = ME(rStock,rIndex,olsbeta(i));
        bayesme(j, i) = ME(rStock,rIndex,olsbeta(i));
        medme(j, i) = ME(rStock,rIndex,medrobustbeta(i));
        trimme(j, i) = ME(rStock,rIndex,trimrobustbeta(i));    
        weightme(j, i) = ME(rStock,rIndex,weightrobustbeta(i));
        shirnkageme(j, i) = ME(rStock,rIndex,shrinkagebeta(i));    
        smoothingtme(j, i) = ME(rStock,rIndex,smoothingbeta);    
        kalmanme(j, i) = ME(rStock,rIndex,kalmanfilterbeta);    
        tobitme(j, i) = ME(rStock,rIndex,tobitmodelbeta(i));    
        trunme(j, i) = ME(rStock,rIndex,truncatedbeta(i));    
        gmmme(j, i) = ME(rStock,rIndex,gmmbeta(i));

        %MSE
        olsmse(j, i) = MSE(rStock,rIndex,olsbeta(i));
        bayesmse(j, i) = MSE(rStock,rIndex,olsbeta(i));    
        medmse(j, i) = MSE(rStock,rIndex,medrobustbeta(i));    
        trimmse(j, i) = MSE(rStock,rIndex,trimrobustbeta(i));
        weightmse(j, i) = MSE(rStock,rIndex,weightrobustbeta(i));
        shirnkagemse(j, i) = MSE(rStock,rIndex,shrinkagebeta(i));
        smoothingtmse(j, i) = MSE(rStock,rIndex,smoothingbeta);
        kalmanmse(j, i) = MSE(rStock,rIndex,kalmanfilterbeta);
        tobitmse(j, i) = MSE(rStock,rIndex,tobitmodelbeta(i));
        trunmse(j, i) = MSE(rStock,rIndex,truncatedbeta(i));
        gmmmse(j, i) = MSE(rStock,rIndex,gmmbeta(i));

        %MAD
        olsmad(j, i) = MAD(rStock,rIndex,olsbeta(i));
        bayesmad(j, i) = MAD(rStock,rIndex,olsbeta(i));
        medmad(j, i) = MAD(rStock,rIndex,medrobustbeta(i));
        trimmad(j, i) = MAD(rStock,rIndex,trimrobustbeta(i));
        weightmad(j, i) = MAD(rStock,rIndex,weightrobustbeta(i));
        shirnkagemad(j, i) = MAD(rStock,rIndex,shrinkagebeta(i));
        smoothingtmad(j, i) = MAD(rStock,rIndex,smoothingbeta);
        kalmanmad(j, i) = MAD(rStock,rIndex,kalmanfilterbeta);
        tobitmad(j, i) = MAD(rStock,rIndex,tobitmodelbeta(i));
        trunmad(j, i) = MAD(rStock,rIndex,truncatedbeta(i));
        gmmmad(j, i) = MAD(rStock,rIndex,gmmbeta(i));

        %R^2
        olsrsquare(j, i) = Rsquare(rStock,rIndex,olsbeta(i));
        bayesrsquare(j, i) = Rsquare(rStock,rIndex,olsbeta(i));
        medrsquare(j, i) = Rsquare(rStock,rIndex,medrobustbeta(i));
        trimrsquare(j, i) = Rsquare(rStock,rIndex,trimrobustbeta(i));
        weightrsquare(j, i) = Rsquare(rStock,rIndex,weightrobustbeta(i));
        shirnkagersquare(j, i) = Rsquare(rStock,rIndex,shrinkagebeta(i));
        smoothingtrsquare(j, i) = Rsquare(rStock,rIndex,smoothingbeta);
        kalmanrsquare(j, i) = Rsquare(rStock,rIndex,kalmanfilterbeta);
        tobitrsquare(j, i) = Rsquare(rStock,rIndex,tobitmodelbeta(i));
        trunrsquare(j, i) = Rsquare(rStock,rIndex,truncatedbeta(i));
        gmmrsquare(j, i) = Rsquare(rStock,rIndex,gmmbeta(i));
    
    end
end

% ME
olsme = mean(mean(olsme));
bayesme = mean(mean(bayesme));
medme = mean(mean(medme));
trimme = mean(mean(trimme));
weightme = mean(mean(weightme));
shirnkageme = mean(mean(shirnkageme));
smoothingtme = mean(mean(smoothingtme));
kalmanme = mean(mean(kalmanme));
tobitme = mean(mean(tobitme));
trunme = mean(mean(trunme));
gmmme = mean(mean(gmmme));

% MSE
olsmse = mean(mean(olsmse));
bayesmse = mean(mean(bayesmse));
medmse = mean(mean(medmse));
trimmse = mean(mean(trimmse));
weightmse = mean(mean(weightmse));
shirnkagemse = mean(mean(shirnkagemse));
smoothingtmse = mean(mean(smoothingtmse));
kalmanmse = mean(mean(kalmanmse));
tobitmse = mean(mean(tobitmse));
trunmse = mean(mean(trunmse));
gmmmse = mean(mean(gmmmse));

% MAD
olsmad = mean(mean(olsmad));
bayesmad = mean(mean(bayesmad));
medmad = mean(mean(medmad));
trimmad = mean(mean(trimmad));
weightmad = mean(mean(weightmad));
shirnkagemad = mean(mean(shirnkagemad));
smoothingtmad = mean(mean(smoothingtmad));
kalmanmad = mean(mean(kalmanmad));
tobitmad = mean(mean(tobitmad));
trunmad = mean(mean(trunmad));
gmmmad = mean(mean(gmmmad));

% R^2
olsrsquare = mean(mean(olsrsquare));
bayesrsquare = mean(mean(bayesrsquare));
medrsquare = mean(mean(medrsquare));
trimrsquare = mean(mean(trimrsquare));
weightrsquare = mean(mean(weightrsquare));
shirnkagersquare = mean(mean(shirnkagersquare));
smoothingtrsquare = mean(mean(smoothingtrsquare));
kalmanrsquare = mean(mean(kalmanrsquare));
tobitrsquare = mean(mean(tobitrsquare));
trunrsquare = mean(mean(trunrsquare));
gmmrsquare = mean(mean(gmmrsquare));
toc;
%     figure('name','ols');
%     plot(olsbeta);
%     figure('name','bayesbeta');
%     plot(bayesbeta);
%     figure('name','med');
%     plot(medrobustbeta);
%     figure('name','trim');
%     plot(trimrobustbeta);
%     figure('name','weight');
%     plot(weightrobustbeta);
%     figure('name','shrinkage');
%     plot(shrinkage);
%     figure('name','tobit');
%     plot(tobitmodelbeta);
%     figure('name','trun');
%     plot(truncatedbeta);
%     figure('name','gmm');
%     plot(gmmbeta);
    
    
    %figure('name','AAA');
    %hold on;
    %plot(olsbeta);
    %plot(smoothingbeta,'r');
    %plot(kalmanfilterbeta,'g');

